package com.hua.yuan;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Toast;

import com.bristua.tencent.rtc.sdk.room.IRtcLoginBusiness;
import com.bristua.tencent.rtc.sdk.room.business.RtcLoginBusinessImpl;
import com.bristua.tencent.rtc.sdk.room.business.callback.IRtcLoginCallback;
import com.bristua.tencent.rtc.sdk.room.business.dao.IRtcLoginDao;
import com.hjm.bottomtabbar.BottomTabBar;
import com.hua.yuan.base.ITabFragment;
import com.hua.yuan.common.permission.PermissionModule;
import com.hua.yuan.fragment.ImageFragment;
import com.hua.yuan.fragment.MeFragment;
import com.hua.yuan.fragment.ShopFragment;
import com.hua.yuan.fragment.VideoFragment;
import com.hua.yuan.live.fragment.LiveFragment;
import com.hua.yuan.userlogin.UserLoginModule;
import com.hua.yuan.userlogin.business.callback.IUserLoginCallback;
import com.hua.yuan.userlogin.business.result.UserSigBean;
import com.hua.yuan.util.SystemUtils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cm.hua.yuan.common.utils.AppContextManager;

public class MainActivity extends AppCompatActivity implements
        IUserLoginCallback,
        PermissionModule.RequestPermissonCallback,
        IRtcLoginCallback
{

    private BottomTabBar mBottomBar;

    private List<ITabFragment> mTabFragments;

    private final String TAG=this.getClass().getName();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTabFragments = new ArrayList<>();
        PermissionModule.getInstance().init(this);
        PermissionModule.getInstance().requestStorage(this);
        initTab();
        initView();
        initData();
    }


    private void initTab() {

        mTabFragments.add(new VideoFragment());
        mTabFragments.add(new ImageFragment());
        mTabFragments.add(new LiveFragment());
        mTabFragments.add(new MeFragment());
        mTabFragments.add(new ShopFragment());
    }


    private void initView() {
        mBottomBar = findViewById(R.id.bottom_bar);
        mBottomBar.init(getSupportFragmentManager(), SystemUtils.getScreenWidth(MainActivity.this), SystemUtils.getScreenHeight(MainActivity.this));
        for (int i = 0; i < mTabFragments.size(); i++) {
            ITabFragment fragment = mTabFragments.get(i);
            mBottomBar.addTabItem(fragment.getTabName(), fragment.getDrawableSelectId(), fragment.getDrawableId(), fragment.getTabFragment().getClass());
        }
    }

    private void initData() {
        UserLoginModule.getInstance().getUserLogin().userLogin(null,null,this);
    }

    @Override
    public void accept(final Boolean aBoolean) {

    }

    @Override
    public void onResult(@NonNull final UserSigBean pUserSign) {
        SystemUtils.isMainThread(TAG);

        if(pUserSign==null){
            return;
        }
        String serverDomin = "https://room.qcloud.com/weapp\n" +
                "/double_room/login";
        IRtcLoginBusiness rtcLoginBusiness=new RtcLoginBusinessImpl();
        Map<String,Object> maps=new HashMap<>();
        maps.put(IRtcLoginDao.SDK_APP_ID,1400120590);
        maps.put(IRtcLoginDao.ACCOUNT_TYPE,"33255");
        maps.put(IRtcLoginDao.RTC_USER_ID,pUserSign.getUid());
        maps.put(IRtcLoginDao.RTC_USER_SIGN,pUserSign.getUsersig());
        rtcLoginBusiness.login(AppContextManager.getInstance().getContext(),maps,this);
    }


    @Override
    public void onError(final Throwable e) {
        SystemUtils.isMainThread(TAG);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getApplicationContext(),"登录失败" , Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void onRtcLoginSuccess(final int errorCdoe) {
        Log.i(TAG,"onRtcLoginSuccess");
        SystemUtils.isMainThread(TAG);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getApplicationContext(),"rtc 登录成功" , Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void onRtcLoginError(final Throwable ex) {
        SystemUtils.isMainThread(TAG);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getApplicationContext(),"登录失败" , Toast.LENGTH_SHORT).show();
            }
        });
    }
}
